package com.chg.mymvc.dao;

import com.chg.mymvc.entity.News;
import com.fJdbcUtil.util.FJdbc;

import java.util.HashMap;
import java.util.List;

public class NewsDaoImpl implements NewsDao {
    @Override
    public void delNews(int id) {
        String sql="delete from news where id=?";
        Object[] objs = new Object[1];
        objs[0]=id;
        FJdbc.update(sql,objs);
    }

    @Override
    public void updateNews(News news) {
        String sql="update news set content=?,title=?,itemid=? where id=?";
        Object[] objs = new Object[4];
        objs[0]=news.getContent();
        objs[1]=news.getTitle();
        objs[2]=news.getItemid();
        objs[3]=news.getId();
        FJdbc.update(sql,objs);
    }

    @Override
    public List<News> queryAll(int page, int pagesize) {
        String sql="select n.id,n.title,n.crtime,n.content,i.perm from news as n,item as i where n.itemid=i.id limit ?,?";
        Object[] params=new Object[]{(page-1)*pagesize,pagesize};
        return FJdbc.queryList(sql,News.class,params);
    }

    @Override
    public List<News> queryAllofperm(int page, int pagesize,String pername) {
        String sql="select n.id,n.title,n.crtime,n.content,i.perm from news as n,item as i where n.itemid=i.id and i.perm="+"'"+pername+"'"+" limit ?,?";
        Object[] params=new Object[]{(page-1)*pagesize,pagesize};
        return FJdbc.queryList(sql,News.class,params);
    }


    @Override
    public int allcount() {
        String sql="select count(id) from news";
        HashMap<String,Object> map = FJdbc.queryOne(sql, HashMap.class);
        long count = (long)map.get("count(id)");
        return new Long(count).intValue();
    }
    @Override
    public News getNewsByName(String name) {
        String sql="select id,title from news where name=?";
        Object[] params=new Object[]{name};
        return FJdbc.queryOne(sql,News.class,params);
    }

    @Override
    public List<News> getNewssByName(String name) {
        String sql="select n.id,n.title,n.crtime,n.content,i.perm from news as n,item as i where n.itemid=i.id and i.perm like ?";
        Object[] params=new Object[]{"%"+name+"%"};
        return  FJdbc.queryList(sql,News.class,params);
    }

//    @Override
//    public News getNewssByName(String name) {
//        String sql="select n.id,n.title,n.crtime,n.content,i.perm from news as n,item as i where n.itemid=i.id and i.perm like ?";
//        Object[] params=new Object[]{"%"+name+"%"};
//        return  FJdbc.queryList(sql,News.class,params);
//    }
    @Override
    public int addNews(News news) {
        String sql="insert into news (title,crtime,content,itemid) values(?,?,?,?)";
        Object[] objs = new Object[4];
        objs[0]=news.getTitle();
        objs[1]=news.getCrtime();
        objs[2]=news.getContent();
        objs[3]=news.getItemid();
        return FJdbc.update(sql, objs);
    }

    @Override
    public News selecNewstById(int id) {
        String sql="select id,title,crtime,content,itemid from news where id=?";
        Object[] objs = new Object[1];
        objs[0]=id;
        return FJdbc.queryOne(sql,News.class,objs);
    }




}
